package cn.itcast.web;

import cn.itcast.mapper.districtMapper;
import cn.itcast.pojo.district;
import cn.itcast.pojo.districtExample;
import cn.itcast.pojo.user_address;
import cn.itcast.service.impl.user_addressServiceImpl;
import cn.itcast.util.OutputStringController;
import cn.itcast.util.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value="/wxshop/user",produces="application/json;charset=UTF-8")
public class user_addressController {
//    @Autowired
//    private DataSource dataSource;
    @Resource
    OutputStringController osc;
    @Resource
    user_addressServiceImpl user_addressService;

    @Resource
    private districtMapper districtMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     *功能：获取所有收货地址
     * @author 欧阳辉
     * @date 2019/5/29
      * @param
     * @return java.lang.String
     */

    @PostMapping("/findAllAddress")
    public String findAllAddress(@RequestBody Map<String,String> map){
        //System.out.println("template.keys(\"*\") = " + template.keys("*"));
        log.debug("获取用户所有收货地址开始");
        //获取openId
        String openId = map.get("openId");
        log.debug("用户唯一标识："+openId);
        //返回结果
        String results ="";
        List<user_address> alladress = user_addressService.findAllAddress(openId);
        if(alladress.isEmpty()){
            results = osc.resultFailure("no","");
            log.debug("获取用户所有收货地址失败");
        }else {
            results = osc.resultSuccess("yes",alladress);
            log.debug("获取用户了所有收货地址成功");
        }

        return results;
    }
    /**
     *功能：增加收货地址
     * @author 欧阳辉
     * @date 2019/5/29
      * @param map
     * @return java.lang.String
     */
    @PostMapping("addAddress")
   public String handleRequest(@RequestBody Map<String,String> map) throws Exception {
        log.debug("添加收货地址开始");

        district county = districtMapper.getByDistrictName(map.get("county"));
        if (county.getIsOpen()==0){
            return osc.failure("此区域暂未开通");
        }


        //获取地址信息
        user_address user_address = new user_address();
        user_address.setUserName(map.get("userName"));
        user_address.setTelephoneNumber(map.get("telephoneNumber"));
        user_address.setProvince(map.get("province"));
        user_address.setCity(map.get("city"));
        user_address.setDetailAddress(map.get("detailAddress"));
        user_address.setOpenId(map.get("openId"));
        user_address.setCounty(map.get("county"));
        user_address.setIsdefault(Integer.parseInt(map.get("isdefault")));
        user_address.setPostcode(map.get("postcode"));
        user_address.setLat(map.get("lat"));
        user_address.setLon(map.get("lon"));

        System.err.println("经纬度啊啊啊："+map.get("lat"));
        System.err.println("经纬度啊啊啊："+map.get("lon"));

//        int code = Integer.parseInt(map.get("code"));
        //返回结果
        String results = "";
        boolean isadd = user_addressService.addUser_address(user_address);
        if(isadd){
            results = osc.success("yes");
            log.debug("收货地址添加成功");
        }else{
            results = osc.failure("no");
            log.debug("收货地址添加失败");
        }

        return results;
    }


    /**
     * 功能描述: <br>
     * 〈〉
     * @Author: lyy
     * @Date: . 12:25
     * @Param: [map]
     * @Return: cn.itcast.util.ResponseEntity
     */
    @RequestMapping(value = "/AddressALL")
    public ResponseEntity AddressALL(){

        districtExample example = new districtExample();
        districtExample.Criteria criteria = example.createCriteria();
        criteria.andIsOpenEqualTo(1);
        List<district> districtList = districtMapper.selectByExample(example);

        if(!districtList.isEmpty()&&districtList.size()!=0){
            return new ResponseEntity(200,"这是你要的数据啊！！！",districtList);
        }else{
            return new ResponseEntity().error_300();
        }
    }

    /*
     *功能：修改收货地址
     * @author 欧阳辉
     * @date 2019/5/29
     * @param map
     * @return java.lang.String
     */
    @PostMapping("updateAddress")
    public String updateAddress(@RequestBody Map<String,String> map){
        log.debug("修改收货地址开始");
        //获取地址信息
        user_address user_address = new user_address();
        String code = "";
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("userName")))){
            user_address.setUserName(map.get("userName"));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("telephoneNumber")))){
            user_address.setTelephoneNumber(map.get("telephoneNumber"));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("province")))){
            user_address.setProvince(map.get("province"));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("city")))){
            user_address.setCity(map.get("city"));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("detailAddress")))){
            user_address.setDetailAddress(map.get("detailAddress"));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("addressId")))){
            user_address.setAddressId(Integer.parseInt(map.get("addressId")));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("county")))){
            user_address.setCounty(map.get("county"));
        }else {
            return osc.failure("no");
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("postcode")))){
            user_address.setPostcode(map.get("postcode"));
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("lat")))){
            user_address.setLat(map.get("lat"));
        }
        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("lon")))){
            user_address.setLon(map.get("lon"));
        }
//        if(StringUtils.isNotBlank(ObjectUtils.toString(map.get("code")))){
//            code = map.get("code");
//        }


        //返回结果
        String results = "";
        boolean isadd = user_addressService.updateUser_address(user_address);
        if(isadd){
            results = osc.success("yes");
            log.debug("收货地址修改成功");
        }else{
            results = osc.failure("no");
            log.debug("收货地址修改失败");
        }

        return results;
    }
    /**
     *功能：删除收货地址
     * @author 欧阳辉
     * @date 2019/5/29
     * @param map
     * @return java.lang.String
     */
    @PostMapping("delectAddress")
    public String delectAddress(@RequestBody Map<String,String> map){
        log.debug("删除收货地址开始");
        //获取地址信息
        String addressId = map.get("addressId");
        user_address user_address = new user_address();
        user_address.setAddressId(Integer.parseInt(addressId));
        //返回结果
        String results = "";
        boolean isdelect = user_addressService.delectUser_address(user_address);
        if(isdelect){
            results = osc.success("yes");
            log.debug("收货地址删除成功");
        }else{
            results = osc.failure("no");
            log.debug("收货地址删除失败");
        }

        return results;
    }


    /**
     *功能：修改默认收货地址
     * @author 欧阳辉
     * @date 2019/5/29
     * @param map
     * @return java.lang.String
     */
    @PostMapping("updateDefaultAddress")
    public String updateDefaultAddress(@RequestBody Map<String,String> map){
        log.debug("修改默认收货地址");
        //获取地址信息
        int addressId = Integer.parseInt(map.get("addressId"));
        String openId = map.get("openId");
        //返回结果
        String results = "";
        boolean isupdate = user_addressService.updateDefaultAddress(addressId,openId);
        if(isupdate){
            results = osc.success("yes");
            log.debug("修改默认收货地址");
        }else{
            results = osc.failure("no");
            log.debug("修改默认收货地址");
        }


        return results;
    }
    /**
     *功能：获取默认收货地址
     * @author 欧阳辉
     * @date 2019/5/29
     * @param map
     * @return java.lang.String
     */
    @PostMapping("getDefaultAddress")
    public String getDefaultAddress(@RequestBody Map<String,String> map){
        log.debug("获取默认收货地址");
        //获取地址信息
        String openId = map.get("openId");
        //返回结果
        String results = "";
        user_address user_address = user_addressService.getDefaultAddress(openId);
        if(user_address!=null){
            results = osc.resultSuccess("yes",user_address);
            log.debug("获取默认收货地址");
        }else{
            results = osc.resultFailure("no","");
            log.debug("获取默认收货地址失败");
        }

        return results;
    }

    @PostMapping("findOneAddress")
    public String findOneAddress(@RequestBody Map<String,String> map){
        log.debug("查询单个收货地址开始");
        //获取地址信息
        Integer addressId = Integer.parseInt(map.get("addressId"));

        //返回结果
        String results = "";
        user_address user_address = user_addressService.findOneUser_address(addressId);
        if(user_address!=null){
            results = osc.resultSuccess("yes",user_address);
            log.debug("查询单个收货地址成功");
        }else{
            results = osc.resultFailure("no","");
            log.debug("查询单个收货地址失败");
        }

        return results;
    }


    /**
     *
     * @Description:
     * 查询区域是否开通
     * @auther: 欧阳辉
     * @date: 10:56 2020/5/10
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("isArea")
    public String isArea(@RequestBody Map<String,String> map){
        log.debug("查询区域是否开通开始");
        //获取地址信息
        String town =map.get("town");

        //返回结果
        String results = "";
        boolean isArea = user_addressService.isArea(town);
        if(isArea){
            results = osc.success("yes");
            log.debug("查询区域是否开通成功");
        }else{
            results = osc.resultFailure("no","");
            log.debug("查询区域是否开通失败");
        }

        return results;
    }


    /**
     *
     * @Description:
     * 获取验证码
     * @auther: 欧阳辉
     * @date: 10:56 2020/5/10
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("getCode")
    public String getCode(@RequestBody Map<String,String> map){
        log.debug("获取验证码开始");

        String phone = map.get("phone");

        //返回结果
        String results = "";
        boolean isArea = user_addressService.getCode(phone);
        if(isArea){
            results = osc.success("yes");
            log.debug("获取验证码成功");
        }else{
            results = osc.resultFailure("no","");
            log.debug("获取验证码失败,请重新发送");
        }

        return results;
    }
}
